package com.qf.bank.service.impl;/*
 * @author     : XXX
 * @date       : 2022/5/5 20:53
 * @description: some description
 */

import com.qf.bank.dao.AccountDao;
import com.qf.bank.dao.ipml.AccountDaoImpl;
import com.qf.bank.entity.Account;
import com.qf.bank.service.AccountService;

import java.math.BigDecimal;

public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao = new AccountDaoImpl();
    @Override
    public Account login(String cardNo, String password) {
        //调用数据访问层

        return accountDao.select(cardNo,password);
    }

    @Override
    public boolean save(String cardNo,BigDecimal money) {
        if (money.doubleValue()<0){
            throw new RuntimeException("输入金额有误");
        }
        accountDao.updateSave(cardNo,money);
        return true;
    }

    @Override
    public boolean take(String cardNo,BigDecimal money) {
        if (money.doubleValue()<0){
            throw new RuntimeException("输入金额有误");
        }
        accountDao.updateTake(cardNo,money);
        return true;
    }

    @Override
    public boolean transMoney(String cardNo, String toCardNo, BigDecimal money) {
        //校验
        //判断是否卡号一致
        if (cardNo.equals(toCardNo)){
            throw new RuntimeException("输入账户与当前账户相同");
        }
        //判断对方账户是否存在
        boolean b = accountDao.selectByCardNo(toCardNo);
        if (!b){
            throw new RuntimeException("对方账户不存在");
        }
        //验证余额是否大于转账金额
        BigDecimal m = accountDao.selectMoney(cardNo);
        if (m.compareTo(money)<0){
            throw new RuntimeException("余额不足");
        }
        //转账
        //我的卡号取钱
        accountDao.updateTake(cardNo,money);
        //对方卡号存钱
        accountDao.updateSave(toCardNo,money);
        return false;
    }
}
